﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	[TestClass]
	public class VariableLength_Test
	{
		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test01_GetUserDataBytes()
		{
			{
				// pass null
				byte[] telegramBytes = null;
				byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
				Assert.IsNull(userDataBytes);
			}

			{
				// pass too short array
				byte[] telegramBytes = new byte[VariableLength.MinLengthOfTelegram - 1];
				byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
				Assert.IsNull(userDataBytes);
			}

			{
				// pass length ok, but wrong length value
				byte[] telegramBytes = new byte[VariableLength.MinLengthOfTelegram + 1];
				byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
				Assert.IsNull(userDataBytes);
			}

			{
				// pass length exactly ok -  no user data bytes
				int nrOfUserDataBytes = 0;
				byte[] telegramBytes = new byte[VariableLength.MinLengthOfTelegram + nrOfUserDataBytes];
				telegramBytes[VariableLength.PositionOfLengthField] = (byte) (2 + nrOfUserDataBytes);
				byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
				Assert.IsNotNull(userDataBytes);
				Assert.AreEqual(nrOfUserDataBytes, userDataBytes.Length);
			}

			{
				// pass length ok - 2 user data bytes
				int nrOfUserDataBytes = 2;
				byte[] telegramBytes = new byte[VariableLength.MinLengthOfTelegram + nrOfUserDataBytes];
				telegramBytes[VariableLength.PositionOfLengthField] = (byte)(2 + nrOfUserDataBytes);
				byte[] userDataBytes = VariableLength.GetUserDataBytes(telegramBytes);
				Assert.IsNotNull(userDataBytes);
				Assert.AreEqual(nrOfUserDataBytes, userDataBytes.Length);
			}
		}
	}
}
